package cn.ym.mall.service.impl;

import cn.ym.mall.dao.RightsDao;
import cn.ym.mall.dao.impl.RightsDaoImpl;
import cn.ym.mall.entity.Rights;
import cn.ym.mall.entity.Role;
import cn.ym.mall.service.RightsService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RightsServiceImpl implements RightsService {
    RightsDao rightsDao = new RightsDaoImpl();

    /**
     * 查询所有权限
     *
     * @return
     */
    @Override
    public List<Rights> getAll() throws SQLException {
        Connection conn = null;
        List<Rights> rightsList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            rightsList = rightsDao.getAll();
        }catch (Exception e){

            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return rightsList;
    }

    /**
     * 添加新权限
     *
     * @param rights
     * @return
     */
    @Override
    public boolean add(Rights rights) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);// 关闭自动提交
            if (rightsDao.add(rights) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据权限id删除权限
     *
     * @param rgid
     * @return
     */
    @Override
    public boolean delete(Integer rgid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);// 关闭自动提交
            if (rightsDao.delete(rgid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 更新现有权限
     *
     * @param rights
     * @return
     */
    @Override
    public boolean update(Rights rights) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);// 关闭自动提交
            if (rightsDao.update(rights) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据权限角色编号查权限
     *
     * @param rigid
     * @return
     */
    @Override
    public Rights getRightsById(Integer rigid) throws SQLException {
        Connection conn = null;
        Rights rights = null;
        try {
            conn = DruidUtils.getConnection();
            rights = rightsDao.getRightsById(rigid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return rights;
    }
}
